package Test;

import mine.ActListener;
import mine.Fish;
import mine.GameFrame;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import static org.junit.Assert.*;

public class ActListenerTest {
    ActListener actListener;
    GameFrame gameFrame;

    @Before
    public void setUp() throws Exception {
        gameFrame = new GameFrame();
        actListener = gameFrame.getActListener();
    }

    @After
    public void tearDown() throws Exception {
        actListener = null;
    }

    @Test
    public void changeButton() {
        actListener.changeButton(true);
        for(JButton b :actListener.getGameFrame().getButtons()){
            assertTrue (b.isVisible());
        }
    }
    @Test
    public void changeButton_2() {
        actListener.changeButton(false);
        for(JButton b :actListener.getGameFrame().getButtons()){
            assertFalse(b.isVisible());
        }
    }

    @Test
    public void beginGame() {
        actListener.beginGame();
        assertEquals(1,actListener.getMyFish().getState());
    }

    @Test
    public void back() {
        actListener.back();
        assert (actListener.getMyFish()==null);
    }

    @Test
    public void game() {
        actListener.game();
        assertTrue(actListener.getMyThread().getName().equals("myThread"));
    }

    @Test
    public void otherFish_1() {
        actListener.setOtherFishs(new ArrayList<Fish>());
        actListener.getOtherFishs().add(new Fish());
        actListener.getOtherFishs().add(new Fish());
        actListener.getOtherFishs().add(new Fish());
        actListener.getOtherFishs().add(new Fish());
        actListener.getOtherFishs().add(new Fish());
        actListener.getOtherFishs().add(new Fish());
        actListener.getOtherFishs().add(new Fish());
        actListener.getOtherFishs().add(new Fish());
        actListener.otherFish();
        assertFalse(actListener.getOtherFishs().size()==9);
    }
    @Test
    public void otherFish_2() {
        actListener.setOtherFishs(new ArrayList<Fish>());
        actListener.otherFish();
        assertTrue(!actListener.getOtherFishs().isEmpty());
    }

    @Test
    public void time() {
        actListener.time();
        assertTrue(actListener.getTimeThread().getName().equals("timeThread"));
    }

    @Test
    public void manipulateRecordTest_1(){
        actListener.setMyFish(new Fish(true));
        actListener.manipulateRecord(1);
        try {
            FileReader reader = new FileReader("src/record1.txt");
            BufferedReader reader1 = new BufferedReader(reader);
            String data = reader1.readLine();
            assertTrue(data.equals("0,0:0"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void manipulateRecordTest_2(){
        actListener.setMyFish(null);
        actListener.manipulateRecord(1);
        assertTrue(actListener.getMyFish()!=null);
    }
}